<?php
/* Globals */
global $target_domain, $target_full_address, $agent_name;
global $rssDescriptionLen, $maxFeedsTotalAccept, $postLink, $rssIncludeHeader;
global $feed_title, $feed_link, $feed_description, $feed_language, $feed_pubdate, $feed_copyright, $feed_webmaster;
global $captured_response, $feedContent;
/* Connection Settings */
$target_domain = "www.facebook.com";	 // Site to contact
$target_full_address = "/PUBLIC-GROUP";	 // Full path to public group wall
$agent_name = $_SERVER['HTTP_USER_AGENT'];
/* RSS Settings */
$rssDescriptionLen = 45;	 // Maximum number of characters to allow in the description
$maxFeedsTotalAccept = 6;	 // Maximum number of links to get
$postLink = "thumbnail";	 // "thumbnail" - URL to thumbnail image, "topic" - URL to topic, "post" - URL to post
$rssIncludeHeader = 1;	 // 1 - include channel information (RSS), 0 - leave channel information out (XML)
$feed_title = "FEED TITLE";
$feed_link = "http://www.facebook.com" . $target_full_address;
$feed_description = "FEED DESCRIPTION";
$feed_language = "en-us";
$feed_pubdate = "11 Apr 2001 01:01:00 GMT";
$feed_copyright = "";
$feed_webmaster = "WEBMASTER@YOUREMAIL.COM";
$captured_response = ""; $feedContent = "";
/* The Crawler */
function botEngine($tdomain, $taddress) {
global $captured_response, $agent_name; $captured_response = "";
$targetURL = "http://" . $tdomain . $taddress;
$response = ""; $ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent_name);
curl_setopt($ch, CURLOPT_URL, $targetURL);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$captured_response = curl_exec($ch);
if (!$captured_response) { $response = "Connection Failed"; }
else { $response = "Connection Successful"; }
return($response);
}
/* Begin */
$postBlockFound = 0; $topicTitleURL = ""; $topicTitle = ""; $postURL = ""; $postUserName = ""; $postTimeStamp = "";
$postUserThumbnail = ""; $postUserContent = ""; $groupPostData = array(); $groupPostDataCount = 0;
$filename = $_SERVER['DOCUMENT_ROOT'] . "/rssDay.txt";
$dayNumber = floor((time()-mktime(null,null,null,1,0,date("Y")))/86400); $foundDay = -1;
if (file_exists($filename)) { $foundDay = (int)file_get_contents($filename, true); }
if ($dayNumber != $foundDay) {
/* Grab Content */
$response = botEngine($target_domain, $target_full_address);
$captured_response_filtered = preg_replace("/(\r\n)+|(\n|\r)+/", "", $captured_response);
$captured_response_filtered = str_replace("\/", "/", $captured_response_filtered);
$captured_response_filtered = str_replace("\\\"", "\"", $captured_response_filtered);
$captured_response_filtered = str_replace("\n", "", $captured_response_filtered);
$lines = explode(">", $captured_response_filtered); $collector = "";
foreach ($lines as $line) {
if ($postBlockFound == 7) {
if ($groupPostDataCount < $maxFeedsTotalAccept) {
$postBlockFound = 1;
$postTimeStamp = $line; $postTimeStamp = str_replace("</a", "", $postTimeStamp);
if (preg_match("/class=\"timestamp\"/i", $postTimeStamp)) {
$tmp = explode("\"", $postTimeStamp); $postTimeStamp = $tmp[3];
}
// Gather Output
$groupPostData[$groupPostDataCount][0] = $feed_link;
$groupPostData[$groupPostDataCount][1] = $feed_title;
$groupPostData[$groupPostDataCount][2] = $postURL;
$groupPostData[$groupPostDataCount][3] = $postUserName;
$groupPostData[$groupPostDataCount][4] = $postTimeStamp;
$groupPostData[$groupPostDataCount][5] = $postUserThumbnail;
$groupPostData[$groupPostDataCount][6] = $postUserContent;
$groupPostDataCount = $groupPostDataCount + 1;
}
// Clear
$postURL = ""; $postUserName = ""; $postTimeStamp = ""; $postUserThumbnail = ""; $postUserContent = "";
}
if ($postBlockFound == 6) { $postBlockFound = 7; }
if ($postBlockFound == 5) {
if (preg_match("/class=\"UIIntentionalStory_Time\"/i", $line)) { $postBlockFound = 6; }
}
if ($postBlockFound == 4) {
$line = str_replace("\n ", "", $line);
if (preg_match("/<\/h3/i", $line)) {
if (strlen($collector) > 2) {
$postBlockFound = 5;
$postUserContent = $collector; $collector = "";
if (strlen($postUserContent) > $rssDescriptionLen) { $postUserContent = substr($postUserContent, 0, $rssDescriptionLen) . "..."; }
}
}
else if (preg_match("/<form /i", $line)) {
$postBlockFound = 5;
$postUserContent = $collector; $collector = "";
if (strlen($postUserContent) > $rssDescriptionLen) { $postUserContent = substr($postUserContent, 0, $rssDescriptionLen) . "..."; }
}
else {
$line = $line . ">";
$flipTitleIndex = 0; $tmp_titleFiltered = "";
for ($step = 0; $step < strlen($line); $step++) {
$tmp_topicChar = substr($line, $step, 1);
if ($tmp_topicChar == "<") { $flipTitleIndex = 1; }
if ($flipTitleIndex == 0) { $tmp_titleFiltered = $tmp_titleFiltered . $tmp_topicChar; }
if ($tmp_topicChar == ">") { $flipTitleIndex = 0; }
}
$collector = $collector . " " . $tmp_titleFiltered;
}
}
if ($postBlockFound == 3) {
if (preg_match("/class=\"UIStory_Message\"/i", $line)) { $postBlockFound = 4; }
}
if ($postBlockFound == 2) {
$postBlockFound = 3;
$tmp = explode("\"", $line);
$postUserThumbnail = $tmp[3];
}
if ($postBlockFound == 1) {
if (preg_match("/class=\"UIIntentionalStory_Pic\"/i", $line)) {
$postBlockFound = 2;
$tmp = explode("\"", $line);
$postURL = $tmp[1];
if (count($tmp) > 4) {
// UserName in href tag
$postUserName = $tmp[5];
}
else {
// UserName out of sync in a span tag
$postUserName = $tmp[3];
}
}
}
if (preg_match("/class=\"minifeedwall\"/i", $line)) { $postBlockFound = 1; }
}
/* Create RSS/XML */
for ($x = 0; $x < $groupPostDataCount; $x++) {
$topicTitleURL = $groupPostData[$x][0];
$topicTitle = $groupPostData[$x][1];
$postURL = $groupPostData[$x][2];
$postUserName = $groupPostData[$x][3];
$postTimeStamp = $groupPostData[$x][4];
$postUserThumbnail = $groupPostData[$x][5];
$postUserContent = $groupPostData[$x][6];
$topicTitleURL = str_replace("&", "&amp;", $topicTitleURL); $postURL = str_replace("&", "&amp;", $postURL);
if (strlen($postUserContent) == 0) { $postUserContent = $topicTitle; }
$postUserContent = str_replace("&", "&", $postUserContent);
$feedContent = $feedContent . "<item>\r\n";
$feedContent = $feedContent . "<title>" . $topicTitle . "</title>\r\n";
if ($postLink == "thumbnail") { $feedContent = $feedContent . "<link>" . $postUserThumbnail . "</link>\r\n"; }
else if ($postLink == "topic") { $feedContent = $feedContent . "<link>" . $topicTitleURL . "</link>\r\n"; }
else { $feedContent = $feedContent . "<link>" . $postURL . "</link>\r\n"; }
$feedContent = $feedContent . "<description>" . $postUserContent . "</description>\r\n";
$feedContent = $feedContent . "<author>" . $postUserName . "</author>\r\n";
$feedContent = $feedContent . "<date>" . $postTimeStamp . "</date>\r\n";
$feedContent = $feedContent . "</item>\r\n";
}
/* Save Generated Data */
$filename = $_SERVER['DOCUMENT_ROOT'] . "/rssCopy.txt";
if (file_exists($filename)) { /*File Found*/ }
else { /*No File*/ }
$filePointer = fopen($filename, "w");
fputs($filePointer, $feedContent);
fclose($filePointer);
/* Save Day Number */
$filename = $_SERVER['DOCUMENT_ROOT'] . "/rssDay.txt";
if (file_exists($filename)) { /*File Found*/ }
else { /*No File*/ }
$filePointer = fopen($filename, "w");
fputs($filePointer, $dayNumber);
fclose($filePointer);
}
else {
/* On current day. Use Local RSS Copy. */
$filename = $_SERVER['DOCUMENT_ROOT'] . "/rssCopy.txt";
$feedContent = file_get_contents($filename, true);
}
/* Generate RSS/XML Output */
if ($rssIncludeHeader == 1) {
header("Content-type: application/rss+xml");
}
else {
header("Content-type: text/xml");
}
print "<" . "?" . "xml version=\"1.0\" encoding=\"utf-8\"" . "?" . ">\r\n";
print "<rss version=\"2.0\">\r\n";
print "<channel>\r\n";
if ($rssIncludeHeader == 1) {
print "<title>" . $feed_title . "</title>\r\n";
print "<link>" . $feed_link . "</link>\r\n";
print "<description>" . $feed_description . "</description>\r\n";
print "<language>" . $feed_language . "</language>\r\n";
print "<date>" . $feed_pubdate . "</date>\r\n";
print "<copyright>" . $feed_copyright . "</copyright>\r\n";
print "<webmaster>" . $feed_webmaster . "</webmaster>\r\n";
}
print $feedContent;
print "</channel>\r\n";
print "</rss>\r\n";
exit;
?>
